﻿using NAudio.Lame;
using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestPrj
{
    public class Mp3Cutter
    {
        public void Execute()
        {
            Console.WriteLine("请输入包含MP3文件的目录路径:");
            string directoryPath = Console.ReadLine();

            if (!Directory.Exists(directoryPath))
            {
                Console.WriteLine("目录不存在!");
                return;
            }

            string[] mp3Files = Directory.GetFiles(directoryPath, "*.mp3");
            if (mp3Files.Length == 0)
            {
                Console.WriteLine("目录中没有找到MP3文件!");
                return;
            }

            string outputDir = Path.Combine(directoryPath, "Trimmed");
            Directory.CreateDirectory(outputDir);

            foreach (string inputFile in mp3Files)
            {
                try
                {
                    string outputFile = Path.Combine(outputDir, Path.GetFileName(inputFile));
                    TrimMp3(inputFile, outputFile, TimeSpan.FromSeconds(19), TimeSpan.FromSeconds(26));
                    Console.WriteLine($"已处理: {Path.GetFileName(inputFile)}");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"处理 {Path.GetFileName(inputFile)} 时出错: {ex.Message}");
                }
            }

            Console.WriteLine("处理完成! 按任意键退出...");
            Console.ReadKey();
        }

        static void TrimMp3(string inputPath, string outputPath, TimeSpan cutFromStart, TimeSpan cutFromEnd)
        {
            using (var reader = new Mp3FileReader(inputPath))
            {
                var totalDuration = reader.TotalTime;
                // 计算开始和结束位置
                var startPos = (long)(reader.WaveFormat.AverageBytesPerSecond * cutFromStart.TotalSeconds);
                var endpos = (long)(reader.WaveFormat.AverageBytesPerSecond * (totalDuration.TotalSeconds - cutFromEnd.TotalSeconds));

                startPos = startPos - (startPos % reader.WaveFormat.BlockAlign);
                endpos = endpos - (endpos % reader.WaveFormat.BlockAlign);

                reader.Position = startPos;
                var bytesToRead = (int)(endpos - startPos);

                var buffer = new byte[1024*1024];

                int bytesRead;
                using (var writer = new LameMP3FileWriter(outputPath, reader.WaveFormat, LAMEPreset.STANDARD))
                {
                    while (bytesToRead > 0 && (bytesRead = reader.Read(buffer, 0, Math.Min(buffer.Length, bytesToRead))) > 0)
                    {
                        writer.Write(buffer, 0, bytesRead);
                        bytesToRead -= bytesRead;
                    }

                }
            }
            /*
                 // 创建裁剪后的文件
             using (var writer = File.Create(outputPath))
             {
                 int bytesRead;
                 while((bytesRead = reader.Read(buffer,0,buffer.Length))>0)
                 {
                     writer.Write(buffer, 0, bytesRead);
                 }

               //  reader.CopyTo(writer, (int)(endPos - startPos) * reader.WaveFormat.AverageBytesPerSecond);
             }
         }
     }*/
        }
    }
}
